package com.example.designmodel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class BuilderActivity extends AppCompatActivity {
    public static void launch(Context context) {
        Intent intent = new Intent(context, BuilderActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_builder);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        builder();
    }

    private void builder(){
//        List<String> sequence = new ArrayList<>();
//        sequence.add(CarModel.START);
//        sequence.add(CarModel.ENERYBOOM);
//        sequence.add(CarModel.STOP);
//        BenChiBuidler buidler = new BenChiBuidler();
//        buidler.setSequence(sequence);
//        BenChiModel benChiModel = (BenChiModel) buidler.getCarModel();
//        benChiModel.run();
//
//        BmwBuilder bmwBuilder = new BmwBuilder();
//        bmwBuilder.setSequence(sequence);
//        BmwModel bmwModel = (BmwModel) bmwBuilder.getCarModel();
//        bmwModel.run();

        Director director = new Director();
        director.getABenChiModel().run();
        director.getBBenChiModel().run();
        director.getABmwModel().run();
        director.getBBmwModel().run();


    }

    public static abstract class CarModel{
        public static final String START = "start";
        public static final String STOP = "stop";
        public static final String ALARM = "alarm";
        public static final String ENERYBOOM = "eneryboom";
        private List<String> sequence = new ArrayList<>();
        abstract void start();
        abstract void stop();
        abstract void alarm();
        abstract void eneryBoom();
        public void run(){
            for (String ss : sequence){
                if(ss.equals(START)){
                    this.start();
                }else if(ss.equals(STOP)){
                    this.stop();
                }else if(ss.equals(ALARM)){
                    this.alarm();
                }else if(ss.equals(ENERYBOOM)){
                    this.eneryBoom();
                }
            }
        }

        final public void setSequence(List<String> sequence){
            this.sequence = sequence;
        }
    }

    public static class BenChiModel extends CarModel{
        @Override
        void start() {
            Log.i("test","BenChiModel start");
        }

        @Override
        void stop() {
            Log.i("test","BenChiModel stop");
        }

        @Override
        void alarm() {
            Log.i("test","BenChiModel alarm");
        }

        @Override
        void eneryBoom() {
            Log.i("test","BenChiModel eneryBoom");
        }
    }

    public static class BmwModel extends CarModel{
        @Override
        void start() {
            Log.i("test","BmwModel start");
        }

        @Override
        void stop() {
            Log.i("test","BmwModel stop");
        }

        @Override
        void alarm() {
            Log.i("test","BmwModel alarm");
        }

        @Override
        void eneryBoom() {
            Log.i("test","BmwModel eneryBoom");
        }
    }

    public static abstract class CarBuidler{
        abstract void setSequence(List<String> sequence);

        abstract CarModel getCarModel();

    }

    public static class BenChiBuidler extends CarBuidler{
        private BenChiModel model = new BenChiModel();
        @Override
        void setSequence(List<String> sequence) {
            model.setSequence(sequence);
        }

        @Override
        CarModel getCarModel() {
            return model;
        }
    }

    public static class BmwBuilder extends CarBuidler{
        private BmwModel model = new BmwModel();
        @Override
        void setSequence(List<String> sequence) {
            model.setSequence(sequence);
        }

        @Override
        CarModel getCarModel() {
            return model;
        }
    }

    public static class Director{
        private BenChiBuidler benChiBuidler = new BenChiBuidler();
        private BmwBuilder bmwBuilder = new BmwBuilder();
        private List<String> sequence = new ArrayList<>();

        public BenChiModel getABenChiModel(){
            sequence.clear();
            sequence.add(CarModel.ALARM);
            sequence.add(CarModel.STOP);
            sequence.add(CarModel.ENERYBOOM);
            benChiBuidler.setSequence(sequence);
            return (BenChiModel) benChiBuidler.getCarModel();
        }

        public BenChiModel getBBenChiModel(){
            sequence.clear();
            sequence.add(CarModel.START);
            sequence.add(CarModel.STOP);
            sequence.add(CarModel.ENERYBOOM);
            benChiBuidler.setSequence(sequence);
            return (BenChiModel) benChiBuidler.getCarModel();
        }

        public BmwModel getABmwModel(){
            sequence.clear();
            sequence.add(CarModel.STOP);
            sequence.add(CarModel.START);
            sequence.add(CarModel.ALARM);
            bmwBuilder.setSequence(sequence);
            return (BmwModel) bmwBuilder.getCarModel();
        }

        public BmwModel getBBmwModel(){
            sequence.clear();
            sequence.add(CarModel.ENERYBOOM);
            sequence.add(CarModel.STOP);
            sequence.add(CarModel.ALARM);
            benChiBuidler.setSequence(sequence);
            return (BmwModel) bmwBuilder.getCarModel();
        }
    }

}
